/**
 * PwdValidator.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年5月22日
 */
package com.pshare.framework.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.constant.SeaxConstants;
import com.pshare.framework.annotation.PwdCheck;

/**
 * 密码格式验证
 */
public class PwdValidator implements ConstraintValidator<PwdCheck, CharSequence> {

	@Override
	public void initialize(PwdCheck constraintAnnotation) {

	}

	@Override
	public boolean isValid(CharSequence object, ConstraintValidatorContext constraintContext) {
		if (StringUtils.isBlank(object))
			return false;
		
		String input = object.toString();
		boolean valid = true;
		if (StringUtils.isBlank(input)
				|| (input.indexOf(SeaxConstants.BLANK) > -1)
				|| input.length() < 5) {
			valid = false;
		}

        if(!valid) {
            constraintContext.disableDefaultConstraintViolation();
            constraintContext.buildConstraintViolationWithTemplate(ReturnMsg.WRONG_FORMAT_PWD).addConstraintViolation();
        }
        
        return valid;
	}

}
